import { Button, Typography } from "@mui/material";
import {
  Box,
  Chip,
  FormControl,
  Grid,
  Paper,
  TextField,
  Dialog,
  DialogActions,
  DialogContent,
  DialogContentText,
  DialogTitle,
  useTheme,
} from "@mui/material";
import React, { useEffect } from "react";
import { useState } from "react";
import { useMutation } from "@tanstack/react-query";
import { postQueryFn } from "../../../query/postQueryFn";
import { useSnackbar } from "notistack";
import { PUT_EXAMSCORE_URL } from "../../../constants/url";

export default function ScoreEdit(props) {
  const [open, setOpen] = useState(false);
  const [totalScore, setTotalScore] = useState();
  const [usualScore, setUsualScore] = useState();
  const [testScore, setTestScore] = useState();
  const [scores, setScores] = useState([]);

  const handleChange1 = (e) => {
    setUsualScore(e);
  };
  const handleChange2 = (e) => {
    setTestScore(e);
  };

  const {
    //成绩上传
    isError: upESError,
    isLoading: upESLoading,
    isSuccess: upESSuccess,
    data: upESdata,
    mutate: upESMutate,
  } = useMutation(["examScores"], postQueryFn);
  const { enqueueSnackbar } = useSnackbar();
  const handleClick = () => {
    if (props.value.selectionModel.length === 0) {
      enqueueSnackbar("还未勾选考生", { variant: "warning" });
      return;
    }
    if (!usualScore) {
      enqueueSnackbar("平时成绩不能为空", { variant: "warning" });
      return;
    }
    if (!testScore) {
      enqueueSnackbar("考试成绩不能为空", { variant: "warning" });
      return;
    }

    //   if(usualScore&&testScore){
    //
    //
    //     setTotalScore(parseInt(usualScore)*0.3+parseInt(testScore)*0.7);
    //
    // }
    let scoresF = scores;
    props.value.selectionModel.map((val, index) =>
      scoresF.push({
        // "usual": 68,
        // "exam": 76,
        // "total": 80,
        // "sid": 1,
        // "id": null
        sid: val,
        usual: parseInt(usualScore),
        exam: parseInt(testScore),
        total: parseInt(usualScore) * 0.3 + parseInt(testScore) * 0.7,
      })
    );
    setScores(...[scoresF]);
    upESMutate({
      Key: [
        PUT_EXAMSCORE_URL,
        {
          params: {
            course: props.value.course,
          },
          useToken: true,
        },
      ],
      method: "put",
      params: scores,
    });
    setOpen(false);
  };
  useEffect(() => {
    if (upESSuccess) {
      if (upESdata.data.code != 0) {
        enqueueSnackbar(upESdata.data.data, { variant: "error" });
      } else {
        enqueueSnackbar("提交成功", { variant: "success" });
        window.location.reload();
      }
    }
  }, [upESSuccess]);
  return (
    <div>
      {props.value.isEnd == false ? (
        <div>
          <Chip label="未结束" color="success" variant="outlined" />
        </div>
      ) : (
        <div>
          <Button color="primary" variant="text" onClick={() => setOpen(true)}>
            成绩录入
          </Button>
          <Dialog
            open={open}
            onClose={() => {
              setOpen(false);
            }}
            aria-labelledby="alert-dialog-title"
            aria-describedby="alert-dialog-description"
          >
            <DialogTitle id="alert-dialog-title">考试成绩录入</DialogTitle>
            <DialogContent sx={{ minWidth: 300, minHeight: 100 }}>
              <DialogContentText id="alert-dialog-description">
                <Box>
                  {/* <Typography >
                        <p>总成绩</p>
                        {totalScore}
                    </Typography> */}
                  <Typography>
                    <p>平时成绩（占比30%）</p>
                  </Typography>
                  <TextField
                    label="平时成绩*"
                    size="small"
                    value={usualScore}
                    sx={{ width: 170 }}
                    onChange={(e) => {
                      handleChange1(e.target.value);
                    }}
                  />
                  <Typography>
                    <p>期末考试成绩（占比70%）</p>
                  </Typography>
                  <TextField
                    label="期末考试成绩*"
                    size="small"
                    value={testScore}
                    sx={{ width: 170 }}
                    onChange={(e) => {
                      handleChange2(e.target.value);
                    }}
                  />
                </Box>
              </DialogContentText>
            </DialogContent>
            <DialogActions>
              <Button variant="contained" onClick={handleClick}>
                保存
              </Button>
            </DialogActions>
          </Dialog>
        </div>
      )}
    </div>
  );
}
